package com.chenfeng.ego.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chenfeng.ego.commons.pojo.EasyUiTree;
import com.chenfeng.ego.commons.pojo.EgoResult;
import com.chenfeng.ego.commons.util.IDUtils;
import com.chenfeng.ego.mapper.TbContentCategoryMapper;
import com.chenfeng.ego.pojo.TbContentCategory;
import com.chenfeng.ego.pojo.TbContentCategoryExample;
import com.chenfeng.ego.service.TbContentCatgroyService;

@Service
public class TbContentCatgroyServiceImpl implements TbContentCatgroyService
{
	@Resource
	TbContentCategoryMapper tbContentCategoryMapper;

	@Override
	// 内容管理树
	public List<EasyUiTree> selById(long id)
	{

		//第一次传过来的id  形成一颗父树  后面的点击在形成子树    （分成层次性）  
		TbContentCategoryExample example = new TbContentCategoryExample();
		example.createCriteria().andParentIdEqualTo(id).andStatusEqualTo(1);
		List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
		//查出来的树的集合放在list集合中
		List<EasyUiTree> easyUiTreeList = new ArrayList<>();
		for (TbContentCategory tbContentCategory : list)
		{
			EasyUiTree ea = new EasyUiTree();
			ea.setId(tbContentCategory.getId());
			ea.setText(tbContentCategory.getName());
			ea.setState(tbContentCategory.getIsParent() ? "closed" : "open");
			easyUiTreeList.add(ea);
		}
		return easyUiTreeList;
	}

	@Override
	// parentId是本子节点的parentId  内容分类的插入  
	public EgoResult insertByid(long parentId, String name)
	{
		EgoResult egoResult = new EgoResult();
		TbContentCategory tbContentCategory = new TbContentCategory();
		tbContentCategory.setCreated(new Date());
		tbContentCategory.setId(IDUtils.genItemId());
		tbContentCategory.setIsParent(false);
		tbContentCategory.setName(name);
		tbContentCategory.setParentId(parentId);
		tbContentCategory.setSortOrder(1);
		tbContentCategory.setStatus(1);
		tbContentCategory.setUpdated(new Date());
		// 判断是否有重名的
		TbContentCategoryExample example = new TbContentCategoryExample();
		example.createCriteria().andNameEqualTo(name);
		int count = this.tbContentCategoryMapper.countByExample(example);
		// 如果小于1则证明其是没有重名的
		if (count < 1)
		{
			// 插入数据
			int result = this.tbContentCategoryMapper.insert(tbContentCategory);
			if (result > 0)
			{
				TbContentCategory parent = new TbContentCategory();
				// 有selecttive的不是全部更新没有的是全部更新
				parent.setId(parentId);
				parent.setIsParent(true);
				int re = this.tbContentCategoryMapper.updateByPrimaryKeySelective(parent);
				// 大于0说明操作成功
				if (re > 0)
				{
					egoResult.setStatus(200);
				} else
				{
					egoResult.setStatus(400);
				}
			} else
			{
				egoResult.setStatus(400);
			}

		} else
		{
			egoResult.setStatus(400);
		}
		return egoResult;
	}

	// 内容删除
	@Override
	public EgoResult deleteByid(long id)
	{
		EgoResult egoResult = new EgoResult();
		TbContentCategory category = new TbContentCategory();
		category.setId(id);
		category.setStatus(2);
		category.setUpdated(new Date());
		int i = this.tbContentCategoryMapper.updateByPrimaryKeySelective(category);
		// 查看删除节点是否成功
		if (i > 0)
		{
			TbContentCategory tb = this.tbContentCategoryMapper.selectByPrimaryKey(id);
			TbContentCategoryExample example = new TbContentCategoryExample();
			// 查询父节点的个数
			example.createCriteria().andParentIdEqualTo(tb.getParentId());
			int count = this.tbContentCategoryMapper.countByExample(example);
			// 当删除的父节点的个数为0时则父节点变成子节点
			if (count < 1)
			{
				TbContentCategory parent = new TbContentCategory();
				parent.setId(tb.getParentId());
				parent.setIsParent(false);
				int re = tbContentCategoryMapper.updateByPrimaryKeySelective(parent);
				if (re > 0)
				{
					egoResult.setStatus(200);
				} else
				{
					egoResult.setStatus(200);
				}
			} else
			{
				egoResult.setStatus(400);
			}

		}else
		{
			egoResult.setStatus(400);
		}
		return egoResult;
	}
}
